// pages/my/index.js
const app = getApp() // 引入app
const _request = require("../../utils/request.js"); // 引入require;
const {
  isObj
} = require("../../utils/util.js"); // 引入require;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    mytabsList: [{
        name: '收藏夹',
        img: 'mytab1.png',
        function: 'toFavorite'
      },
      {
        name: '优惠券',
        img: 'mytab2.png',
        function: 'toMypreferentialcard'
      },
      {
        name: '钱包',
        img: 'mytab3.png',
        function: 'toWallet'
      },
      {
        name: '意豆',
        img: 'mytab4.png',
        function: 'toYidou'
      },
    ],
    makerList: [{
        name: '订单收益',
        img: 'maker1.png'
      },
      {
        name: '现金大礼包',
        img: 'maker2.png'
      },
      {
        name: '推荐奖励',
        img: 'maker3.png'
      },
      {
        name: '商品带货',
        img: 'maker4.png'
      },
      {
        name: '更多',
        img: 'maker5.png'
      },
    ],
    myserverList: [{
        name: '收货地址',
        img: 'address.png',
        function: 'toSite'
      },
      {
        name: '账户安全',
        img: 'safety.png',
        function: 'toMyset'
      },
      {
        name: '推广分享',
        img: 'promotion.png',
        function: 'toGeneralize'
      },
      // {
      //   name: '推广红包',
      //   img: 'promotion-hb.png',
      //   function: 'toRedenvelope'
      // },
      {
        name: '卡券兑换',
        img: 'card.png',
        function: 'toCard'
      },
      {
        name: '消费保障',
        img: 'tool2.png',
        function: 'toSafeguard'
      },
      {
        name: '问题/反馈',
        img: 'tool3.png',
        function: 'toFeedback'
      },
      {
        name: '关于意约',
        img: 'tool4.png',
        function: 'toAbout'
      },
    ],
    shoporderList: [{
        name: '待付款',
        type:0,
        sum: '0'
      },
      {
        name: '待发货',
        type:1,
        sum: '0'
      },
      {
        name: '待收货',
        type:2,
        sum: '0'
      },
      {
        name: '已完成',
        type:3,
        sum: '0'
      }
    ],
    markershow: false,
    codeObj: null,
    userObj: null,
    makerObj: null,
    OrderCountList: null,
    seeShow: true,
    active: 0,
    accountObj: null,
    momentumReturns: 0
  },
  // 我的资料
  onMyProfile(){
    wx.navigateTo({
      url: '/pages/myprofile/index'
    })
  },
  // 前往登录
  onLogin() {
    wx.reLaunch({
      url: '/pages/login/index',
    })
  },

  // 领取创客卡
  getMarkerCard() {
    wx.navigateTo({
      url: '/pages/maker/buymaker/index',
    })
  },

  // 查看详情创客卡
  getMarkerDetails() {
    wx.navigateTo({
      url: '/pages/maker/makerdetails/index?title=' + this.data.makerDetail.description,
    })
  },

  // 前往传创客详情
  onMaker(){
    if(this.data.makerObj.ismaker){
      wx.navigateTo({
        url: '/pages/maker/makerdetails/index?title=' + this.data.makerDetail.description,
      })
    }else{
      wx.navigateTo({
        url: '/pages/maker/buymaker/index',
      })
    }
  },
  // 点击展示更多创客内容
  showMarker() {
    this.setData({
      markershow: !this.data.markershow
    })
  },

  // 点击查看和隐藏金额
  seeMoney() {
    this.setData({
      seeShow: !this.data.seeShow
    })
  },


  // 前往对应页面
  onOrdertype(e) {
    const type = e.currentTarget.dataset.type;
    app.globalData.orderactive = type;
    wx.navigateTo({
      url: '/pages/mallorder/orderlist?type='+ type
    })
  },

  // 复制邀请码
  copywxtap() {
    wx.showToast({
      title: '复制成功',
    })
    let _this = this
    wx.setClipboardData({
      data: this.data.codeObj.invitecode,
      success: function (res) {
        wx.getClipboardData({
          //这个api是把拿到的数据放到电脑系统中的
          success: function (res) {
            console.log(res.data) // data
          }
        })
      }
    })
  },

  // 我的收藏
  toFavorite() {
    wx.navigateTo({
      url: '/pages/myfavorite/index',
    })
  },

  // 我的优惠卷
  toMypreferentialcard() {
    wx.navigateTo({
      url: '/pages/mypreferentialcard/index',
    })
  },

  // 我的钱包
  toWallet() {
    wx.navigateTo({
      url: '/pages/wallet/index',
    })
  },

  // 我的意豆
  toYidou() {
    wx.navigateTo({
      url: '/pages/yidou/index',
    })
  },

  // 我的收益
  toIncome() {
    wx.navigateTo({
      url: '/pages/myincome/index',
    })
  },
  // 签到
  goSigin() {
    wx.navigateTo({
      url: '/pages/sigin/index',
    })
  },

  // 推广红包
  toRedenvelope() {
    wx.navigateTo({
      url: '/pages/redenvelope/index',
    })
  },

  // 卡劵兑换
  toCard() {
    wx.navigateTo({
      url: '/pages/cardexchange/index',
    })
  },

  // 关于意约
  toAbout() {
    wx.navigateTo({
      url: '/pages/aboutus/index',
    })
  },

  // 收货地址
  toSite() {
    wx.navigateTo({
      url: '/pages/site/index',
    })
  },

  // 账户安全
  toMyset() {
    wx.navigateTo({
      url: '/pages/myset/index',
    })
  },

  // 消费保障
  toSafeguard() {
    wx.navigateTo({
      url: '/component/safeguard/index',
    })
  },

  // 问题反馈
  toFeedback() {
    wx.navigateTo({
      url: '/component/feedback/index',
    })
  },

  // 推广分享
  toGeneralize() {
    wx.navigateTo({
      url: '/pages/generalize/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取我的推荐码
    _request.get('/recomUser/findReCode', {
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      this.setData({
        codeObj: res.data.data
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    // 获取用户个人信息
    _request.get('/storeCenterInfo/getStoreCenterInfo', {
      id: wx.getStorageSync('userid')
    }).then((res) => {
      this.setData({
        userObj: res.data.data,
      })
    }).catch((error) => {
      console.log(error, 'error');
    })

    // 是否创客
    _request.get('/maker/checkIsMaker', {
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      let vm = this
      vm.setData({
        makerObj: res.data.data
      })
      // 是创客的时候才去获取详情
      if (res.data.data.ismaker) {
        // 获取用户创客信息
        _request.get('/maker/userMakerInfo', {
          userId: wx.getStorageSync('userid')
        }).then((res) => {
          let data = res.data.data
          let myupgrade
          // 计算当前升级所需人数
          data.makerUpRule.map((item,index) => {
            if (data.description == item.description && item.description != '传奇创客') {
              myupgrade = data.makerUpRule[index + 1].upgradeNum - data.downBuySum
            }else if(data.description == '传奇创客'){
              myupgrade = 3000
            }
            return item
          })
          if (res.data.code == 200) {
            vm.setData({
              makerDetail: {
                ...res.data.data,
                myupgrade
              }
            })
          }
        }).catch((error) => {
          console.log(error, 'error');
        })
      }
      wx.setStorageSync('maker', res.data.data)
    }).catch((error) => {
      console.log(error, 'error');
    })

    // 获取账户余额信息
    _request.get('/wallet/getPersonAccount', {
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      this.setData({
        accountObj: res.data.data
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})